package com.ctgu.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ctgu.springboot.common.QueryPageParam;
import com.ctgu.springboot.common.Result;
import com.ctgu.springboot.entity.Student;
import com.ctgu.springboot.entity.StudentVO;
import com.ctgu.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzs
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //新增学生
    @PostMapping("/save")
    public Result save(@RequestBody Student student) {
        if(StringUtils.isBlank(student.getStuPwd()) || "null".equals(student.getStuPwd())){
            student.setStuPwd("123456");
        }
        return studentService.save(student) ? Result.suc() : Result.fail();
    }

    //更新学生信息
    @PostMapping("/update")
    public Result update(@RequestBody Student student) {
        return studentService.updateById(student) ? Result.suc() : Result.fail();
    }


    //删除学生信息
    @GetMapping("/del")
    public Result del(@RequestParam String stu_id) {
        return studentService.removeById(stu_id) ? Result.suc() : Result.fail();
    }


    //分页查询学生信息
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        IPage<Student> result = studentService.studentPage(query);
        return Result.suc(result.getRecords(), result.getTotal());
    }

    //@GetMapping("list")
    //public List<Teacher> list(){
    //    return teacherService.list();
    //}
    //

    //@PostMapping("getById")
    //public Teacher getById(@RequestParam String id){
    //    return teacherService.getById(id);
    //}

    @RequestMapping("login")//学生登录
    public Result getStuInfoByPassword(int stu_phone, String stu_pwd){
        QueryWrapper<Student> studentQueryWrapper=new QueryWrapper<>();
        studentQueryWrapper.eq("stu_phone",stu_phone)
                .eq("stu_pwd",stu_pwd);
        if(studentService.getOne(studentQueryWrapper)!=null){
            Student stuInfo=studentService.getOne(studentQueryWrapper);
            if(studentService.getInfo(stuInfo.getStuId())!=null){
                Map<String,Object> resultMap = new HashMap<>();
                StudentVO studentVO = studentService.getInfo(stuInfo.getStuId());
                String token = UUID.randomUUID().toString();
                resultMap.put("student",studentVO);
                resultMap.put("token", token);
                return Result.suc(resultMap);
            }else{
                return Result.fail();
            }
        }else{
            return Result.fail();
        }

    }

    @RequestMapping("findteamid")//根据队伍查找学生
    public List<Student> findteamid(int team_id){
        QueryWrapper<Student> studentQueryWrapper=new QueryWrapper<>();
        studentQueryWrapper.eq("team_id",team_id);
        return studentService.list(studentQueryWrapper);
    }

    //获取所有学生信息
    @GetMapping("getList")
    public List<Student> getList(){
        return studentService.getList();
    }

    @GetMapping("list")
    public List<Student> list(){
        return studentService.list();
    }

    //@GetMapping("getTeamName")
    //public String getTeamName(@RequestParam Integer stu_id){
    //    return studentService.getTeamName(stu_id);
    //}


}
